package com.qa.controller.user;

import com.qa.constant.QuestionConstant;
import com.qa.dto.DeleteManyDTO;
import com.qa.dto.QuestionAddDTO;
import com.qa.dto.QuestionQueryDTO;
import com.qa.entity.Question;
import com.qa.result.Result;
import com.qa.service.QuestionService;
import com.qa.utils.UserUtil;
import com.qa.vo.QuestionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 徐
 */
@Api(tags = "用户端题目接口")
@Slf4j
@RestController
@RequestMapping("/user/questions")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @PostMapping("/condition/my")
    @ApiOperation(value = "条件查询该用户所有的题")
    public Result<List<QuestionVO>> getAllMyQuestion(@RequestBody QuestionQueryDTO questionQueryDTO) {
        log.info("questionQueryDTO:{}", questionQueryDTO);
        // 获取用户id
        Long userId = UserUtil.getCurrentUserId();
        // 设置创建者id（为了只查询我的题目）
        questionQueryDTO.setCreator(userId);
        // 未删除的题目
        questionQueryDTO.setStatus(QuestionConstant.STATUS_EXISTENCE);
        List<QuestionVO> questions = questionService.getQuestionByCondition(questionQueryDTO);
        return Result.success(questions);
    }

    @PostMapping("/condition/all")
    @ApiOperation(value = "条件查询全站用户所有的题")
    public Result<List<QuestionVO>> getAllQuestion(@RequestBody QuestionQueryDTO questionQueryDTO) {
        // 获取用户id
        Long userId = UserUtil.getCurrentUserId();
        // 设置创建者id（为了不查询我的题目）
        questionQueryDTO.setCreator(userId);
        // 未删除的题目
        questionQueryDTO.setStatus(QuestionConstant.STATUS_EXISTENCE);
        // 公开的题目
        questionQueryDTO.setOpen(QuestionConstant.OPEN);
        List<QuestionVO> questions = questionService.getQuestionNotUserByCondition(questionQueryDTO);
        return Result.success(questions);
    }

    @PostMapping
    @ApiOperation(value = "新增题目")
    public Result addChoiceQuestion(@RequestBody QuestionAddDTO questionAddDTO) {
        return questionService.addQuestion(questionAddDTO);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "修改题目")
    public Result editQuestion(@RequestBody Question question) {
        return questionService.editQuestion(question);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除题目")
    public Result deleteQuestion(@PathVariable Long id) {
        return questionService.deleteQuestion(id);
    }

    @PostMapping("/delete/many")
    @ApiOperation(value = "批量删除题目")
    public Result deleteQuestion(@RequestBody DeleteManyDTO deleteManyDTO) {
        return questionService.deleteQuestions(deleteManyDTO.getIds());
    }
}
